#include "StdAfx.h"
#include "UserInterface.h"

const string UserInterface::MESSAGE[12] = {"%s is added to Tasker.", 
                "Tasks for %s are cleared.", 
                "%s is deleted from Tasker.", 
                "%s is marked as done.",
                "There are no tasks in Tasker.", 
                "invalid command: %s", 
                "%s is an invalid date.",
                "%s is an invalid integer.",
                "There is no task \"%s\" in Tasker.",
                "Task %s is not listed.",
                "%s is an invalid time",
                "No results are found for %s", 
        };

const std::string UserInterface::MESSAGE_WELCOME = "Welcome to Tasker.\nFor more information on the list of commands available, type \"help\"\nPlease enter your command below.";

UserInterface::UserInterface(void)
{
}


UserInterface::~UserInterface(void)
{
}

bool UserInterface::stringObjToStdConversion(System::String^ source, string& result)
{
        int len = ((source->Length+1)*2);
        char* temp = new char[len];

        pin_ptr<const wchar_t> wch = PtrToStringChars( source );
        bool success = wcstombs( temp, wch, len ) != -1;

        result = temp; 
        delete temp; 
        return success;
}

string UserInterface::generateWelcomeMessage(void)
{
        return MESSAGE_WELCOME;
}

string UserInterface::readUserInput(System::String^ input)      //for when multiple controls on form
{
        string command;
        if(stringObjToStdConversion(input, command))
                return trim(command);
        else
                return "";
}

void UserInterface::startingExecutor(void)
{
        Executor execute;
        execute.loadVectToExecutor();
}

string UserInterface::sendingInputToExecutor(string& input)
{
        Executor execute;
        return execute.executeCommand(input);
}

string UserInterface::generateMessage(string msg, string param)
{
        MESSAGE_TYPE message_t = determineMessageType(msg);
        char buffer[255];
        sprintf_s(buffer, MESSAGE[message_t].c_str(), param.c_str());
        return buffer;
}

UserInterface::MESSAGE_TYPE UserInterface::determineMessageType(string msg)
{
        if(msg == "added") {
            return MESSAGE_TYPE::ADDED;
                } else if (msg == "deleted") {
            return MESSAGE_TYPE::DELETED;
        } else if (msg == "invalidTaskNum") {
            return MESSAGE_TYPE::INVALID_TASK_NO;
        } else if (msg == "notInt") {
            return MESSAGE_TYPE::INVALID_INT;
        } else if (msg == "isEmptyFile") {
            return MESSAGE_TYPE::EMPTY_FILE;
        } else if (msg == "invalidDate") {
            return MESSAGE_TYPE::INVALID_DATE;
        } else if (msg == "matchUnfound") {
            return MESSAGE_TYPE::MATCH_UNFOUND;
        } else if (msg == "cleared") {
                return MESSAGE_TYPE::CLEARED;
        } else if (msg == "Done") {
                return MESSAGE_TYPE::DONE;
        } else if (msg == "invalidTime") {
                return MESSAGE_TYPE::INVALID_TIME;
        } else if (msg == "invalidName") {
                return MESSAGE_TYPE::INVALID_TASK;
        } else {
			return MESSAGE_TYPE::INVALID_CMD;
        } 
}

string UserInterface::generateHelpList(void)
{
        string cmdHelp = "add [task name] -date [dd/mm/yyyy] or -starts [hhmm] or -ends [hhmm] or -detail [detail]\n";
        cmdHelp += "delete [task number]\n";
        cmdHelp += "show \n";
        cmdHelp += "show impt\n";
        cmdHelp += "show detail\n";
        cmdHelp += "show [dd/mm/yyyy]\n";
        cmdHelp += "search [part of/full task name]\n";
        cmdHelp += "clear [dd/mm/yyyy]\n";
        cmdHelp += "done [task number]\n";
        return cmdHelp;
}

//#pragma region checkBoxList
//
//DataTable^ UserInterface::getTaskList(string date, vector<int> fileIndex, vector<string> subject, vector<bool> done)
//{
//	DataTable^ taskList;
//	generateTableColumns(/*create an enumerator for format of datatable*/ CHECKEDBOXLIST, taskList);
//	for(int i = 0; i < subject.size(); i++)
//	{
//		DataRow^ oneTask = taskList->NewRow();
//		oneTask->default["index"] = fileIndex[i];
//		oneTask->default["subject"] = subject[i];
//		oneTask->default["done"] = done[i];
//		taskList->Rows->Add(oneTask);
//	}
//	return taskList;
//}
//
///*
//DataTable UserInterface::printTaskList(vector<Task, int> taskList)
//{
//	if(taskList.empty())
//	{
//		throw exception("No Task Retrieved");
//	}
//	DataTable displayTable;
//	displayTable.Columns->Add("displayIndex");
//	displayTable.Columns->Add("storageIndex");
//	displayTable.Columns->Add("subject");
//	displayTable.Columns->Add("date");
//	displayTable.Columns->Add("start time");
//	displayTable.Columns->Add("end time");
//	displayTable.Columns->Add("done");
//	for(int i = 0; i < taskList.size(); i++)
//	{
//		DataRow^ oneTask = displayTable.NewRow();
//		oneTask->default[0] = i;
//		oneTask->default[1] = taskList[i][1];
//		oneTask->default[2] = taskList[i][0].getName();
//		oneTask->default[3] = taskList[i][0].getDate();
//		oneTask->default["start time"] = taskList[i][0].getStartTime();
//		oneTask->default["end time"] = taskList[i][0].getEndTime();
//		oneTask->default["done"] = taskList[i][0].getDone();
//		displayTable.Rows->Add(oneTask);
//	}
//	return displayTable;
//}
//*/
//#pragma endregion

inline string UserInterface::trim(const string& s, const string& delimiters) 
{
        if(!s.empty())
        {
                int tail = s.find_last_not_of(delimiters) + 1;
                int head = s.find_first_not_of(delimiters);
                if((head == string::npos) || (tail == string::npos))
                        return "";
                return s.substr(head, tail);
        }
        else
                return s;
}